package com.Lixiaoqian.CardPlay.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;

import static com.Lixiaoqian.CardPlay.utils.Constant.CHOOSE_PICTURE;
import static com.Lixiaoqian.CardPlay.utils.Constant.CROP_SMALL_PICTURE;
import static com.Lixiaoqian.CardPlay.utils.Constant.TAKE_PICTURE;
import static com.Lixiaoqian.CardPlay.utils.Constant.uritempFile;

/**
 * Created by HuangZhiBo on 2017/4/1/001.
 */

public class CameraCutUtils {

    /**
     * 显示修改头像的对话框
     */
    public static  void showChoosePicDialog(final Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("设置头像");
        String[] items = {"选择本地照片", "拍照"};
        builder.setNegativeButton("取消", null);
        builder.setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case CHOOSE_PICTURE: // 选择本地照片
                        Intent openAlbumIntent = new Intent(
                                Intent.ACTION_GET_CONTENT);
                        openAlbumIntent.setType("image/*");
                        activity.startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
                        break;
                    case TAKE_PICTURE: // 拍照
                        Intent openCameraIntent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                        Constant.uritempFile = Uri.fromFile(new File(Environment
                                .getExternalStorageDirectory(), Constant.IMAGENAME));
                        // 指定照片保存路径（SD卡），image.jpg为一个临时文件，每次拍照后这个图片都会被替换
                        openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Constant.uritempFile);
                        activity.startActivityForResult(openCameraIntent, TAKE_PICTURE);
                        break;
                }
            }
        });
        builder.create().show();
    }


    /**
     * 裁剪图片方法实现
     * @param uri
     */
    public static void startPhotoZoom(Uri uri,final Activity activity) {
        if (uri == null) {
            //  Log.i("tag", "The uri is not exist.");
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 800);
        intent.putExtra("outputY", 800);
        // intent.putExtra("return-data", true);
        intent.putExtra("scale", true);
        intent.putExtra("scaleUpIfNeeded", true);
        //uritempFile为Uri类变量，实例化uritempFile 直接裁剪后保存图片到本地指定路径
        uritempFile = Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/" + "image.png");
        LogUtils.showLog(uritempFile.toString());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uritempFile);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        activity.startActivityForResult(intent, CROP_SMALL_PICTURE);
    }

    /**
     * 将裁剪后的图片设置到view和本地文件
     */
    public static void setImageToView(final Activity activity, ImageView imageView) {
        Bitmap bitmap=null;
        try {
            bitmap = BitmapFactory.decodeStream(activity.getContentResolver().openInputStream(uritempFile));
        } catch (FileNotFoundException e) {
            ToastUtils.showShort(activity,"用户图片获取失败");
        }
        if (bitmap!=null) {
            imageView.setImageBitmap(bitmap);
            Constant.is_changeImg = true;
        }

    }
}
